<template>
    <view>
        <view class="lines">
            <radio-group @change="radioChange">
                <view class="line" v-for="(ledger,k) of ledgers" :key="k">
                    <label style="display: flex">
                        <radio :checked="ledgerD.id===ledger.id" :value="ledger.name" style="transform:scale(0.65,0.65)" color="#08BA88"/>
                        <text>{{ledger.name}}</text>
                        <button v-if="k===0" open-type="share">分享</button>
                        <button v-else @click.stop="shareBook">分享</button>
                    </label>
                </view>
            </radio-group>
        </view>
        <view style="height: 144rpx"/>
        <view class="bottom" @click="ledgerChange" v-if="ledgers.length>0">
            <view class="btn">切换</view>
        </view>
    </view>
</template>

<script>
import { mapState, mapMutations } from 'vuex'
import { shareMixin } from '../../common/mixins'
import { httpPOST } from '@/common/req'

export default {
  name: 'book',
  data() {
    return {
      ledgerD: {},
      share: {
        title: '邀请你加入我的共享账本',
        path: '/main/book/share',
        imageUrl: 'https://s.benlv.top/upload/1715406443-1.png'
      }
    }
  },
  mixins: [shareMixin],
  watch: {
    ledgers: {
      handler(e) {
        this.ledgerD = e.find(i => i.id === this.userInfo.b_id)
      },
      deep: true
    }
  },
  onLoad() {
    this.fetchLedgers()
  },
  computed: {
    ...mapState(['ledgers', 'userInfo'])
  },
  methods: {
    ...mapMutations(['fetchLedgers', 'updateLedger', 'loginCheck', 'fetchLedgerLogo']),
    async shareBook() {
      const url = 'https://api.benlv.top/auth/getActivity'
      const [err, res] = await httpPOST({ url, param: {} })
      console.log(err,res)

      // wx.updateShareMenu({
      //   withShareTicket: true,
      //   isPrivateMessage: true,
      //   activityId: 'xxx',
      //   success: (e) => {
      //     console.log('success', e)
      //   },
      //   fail: (e) => {
      //     console.log('fail', e)
      //   }
      // })
    },
    radioChange({ detail: { value } }) {
      this.ledgerD = this.ledgers.find(i => i.name === value)
    },
    ledgerChange() {
      this.updateLedger({
        b_id: this.ledgerD.id,
        call: () => {
          this.$api.alert('切换成功')
          uni.switchTab({ url: '/main/Home/Home' })
          this.fetchLedgers()
          this.fetchLedgerLogo()
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
